// test_HcommStub.cpp
#include "hccl_api.h"
#include "EndpointStore.h"

extern "C" {

static int g_endpoint_create_count = 0;
static int g_endpoint_destroy_count = 0;
static int g_channel_create_count = 0;
static int g_channel_destroy_count = 0;

hccl::HcclResult HcommEndPointCreate(const hccl::EndPoint *endPoint,
                                     hixl::EndpointHandle *endPointHandle)
{
    ++g_endpoint_create_count;
    // 用 new 出一块内存做“句柄”，方便 Destroy 时验证
    *endPointHandle = reinterpret_cast<hixl::EndpointHandle>(
        new hccl::EndPoint(*endPoint));
    (void)endPoint;
    return hccl::HcclResult::SUCCESS;
}

hccl::HcclResult HcommEndPointDestroy(hixl::EndpointHandle endPointHandle)
{
    ++g_endpoint_destroy_count;
    delete reinterpret_cast<hccl::EndPoint*>(endPointHandle);
    return hccl::HcclResult::SUCCESS;
}

hccl::HcclResult HcommChannelCreate(hixl::EndpointHandle *endPointHandle,
                                    uint32_t /*engine*/,
                                    void * /*channelDescList*/,
                                    uint32_t listNum,
                                    const void ** /*memHandleList*/,
                                    uint32_t /*memHandleListNum*/,
                                    hixl::ChannelHandle *channelList)
{
    ++g_channel_create_count;
    // 简单给一个非空句柄
    for (uint32_t i = 0; i < listNum; ++i) {
        channelList[i] = reinterpret_cast<hixl::ChannelHandle>(
            static_cast<uintptr_t>(i + 1));
    }
    (void)endPointHandle;
    return hccl::HcclResult::SUCCESS;
}

hccl::HcclResult HcommChannelDestroy(const hixl::ChannelHandle * /*channelList*/,
                                     uint32_t listNum)
{
    ++g_channel_destroy_count;
    (void)listNum;
    return hccl::HcclResult::SUCCESS;
}

hccl::HcclResult HcommMemImport(hixl::EndpointHandle /*endPointHandle*/,
                                const void * /*memDesc*/,
                                uint32_t /*descLen*/,
                                void *outBuf)
{
    auto *buf = reinterpret_cast<hccl::HcommBuf*>(outBuf);
    buf->addr = reinterpret_cast<void*>(0x1234);
    buf->len  = 4096;
    return hccl::HcclResult::SUCCESS;
}

} // extern "C"

// 方便 UT 里读计数：
int GetEndpointCreateCount()  { return g_endpoint_create_count; }
int GetEndpointDestroyCount() { return g_endpoint_destroy_count; }
int GetChannelCreateCount()   { return g_channel_create_count; }
int GetChannelDestroyCount()  { return g_channel_destroy_count; }
